import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/pages/web_tabs/market/market_list.dart';

class GetPassword extends StatefulWidget {
  GetPassword({super.key});

  @override
  State<GetPassword> createState() => _SettingPageState();
}

class _SettingPageState extends State<GetPassword> {
  final RxBool _isLogin = true.obs;
  bool _obscureText = true;

  RxInt codetext = 60.obs;
  RxBool butdisabled = false.obs;

  RxBool valgroup = true.obs;

  final TextEditingController code = TextEditingController();
  final TextEditingController phone = TextEditingController(text: '');
  final TextEditingController controller = TextEditingController(text: '');
  final TextEditingController controller1 = TextEditingController(text: '');

  @override
  void dispose() {
    code.dispose();
    phone.dispose();
    controller.dispose();
    controller1.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Expanded(
          child: ListView(
            children: [
              Stack(
                children: [
                  SafeArea(
                    top: false,
                    child: Image.asset(
                      'static/images/dengli.png',
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                  // Positioned(
                  //   bottom: 0,
                  //   right: 0,
                  //   left: 0,
                  //   child: Image.asset(
                  //     'static/images/loginimgb.png',
                  //     fit: BoxFit.fitWidth,
                  //   ),
                  // ),
                  // Positioned(
                  //   bottom: 20,
                  //   left: 60,
                  //   right: 60,
                  //   child: Row(
                  //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  //     children: [
                  //       Text('手机找回',
                  //           style: TextStyle(
                  //               fontSize: 18, color: Color(0XFF999999))),
                  //       Text('邮箱找回',
                  //           style: TextStyle(
                  //               fontSize: 18, color: Color(0XFF999999))),
                  //     ],
                  //   ),
                  // ),
                ],
              ),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                decoration: BoxDecoration(
                    border:
                        Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                child: Row(
                  children: [
                    SizedBox(
                      width: 15,
                    ),
                    Image.asset(
                      'static/images/guoqi.png',
                      fit: BoxFit.fitWidth,
                      width: 30,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text('+86', style: TextStyle(fontSize: 14)),
                    Expanded(
                        child: TextField(
                          controller: phone,
                      style: TextStyle(fontSize: 14),
                      decoration: InputDecoration(
                        isCollapsed: true, //设置高度
                        contentPadding: EdgeInsets.all(15),
                        hintStyle: TextStyle(color: Color(0xff999999)),
                        border: InputBorder.none, // 隐藏输入框默认边框
                        hintText: '请输入手机号', // 提示文本
                      ),
                    ))
                  ],
                ),
              ),
              Container(
                  margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  decoration: BoxDecoration(
                      border:
                          Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          controller: code,
                          style: TextStyle(fontSize: 14),
                          decoration: InputDecoration(
                            isCollapsed: true, //设置高度
                            contentPadding: EdgeInsets.all(15),
                            hintStyle: TextStyle(color: Color(0xff999999)),
                            border: InputBorder.none, // 隐藏输入框默认边框
                            hintText: '请输入验证码', // 提示文本
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
                          if (phone.text.isNotEmpty) {
                            if (butdisabled.value) {
                              return;
                            }
                            Get.find<HTTPS>().postData('/phone-code', {
                              "phone": phone.text,
                              "value": "password",
                            }).then((res) {
                              if (res != null) {
                                Get.find<HTTPS>().showTost('发送成功');
                                butdisabled.value = true;
                                codetext.value = 60;

                                Timer.periodic(Duration(seconds: 1), (timer) {
                                  codetext.value--;
                                  if (codetext.value < 1) {
                                    codetext.value = 1;
                                    timer.cancel();
                                    butdisabled.value = false;
                                  }

                                  // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                                });
                              }
                            });
                          } else {
                            Get.find<HTTPS>().showTost('输入手机号');
                          }
                        },
                        child: Obx(() => butdisabled.value
                            ? Text('已发送$codetext',
                                style: TextStyle(
                                    fontSize: 12, color: Color(0XFF0A72D9)))
                            : Text('发送验证码',
                                style: TextStyle(
                                    fontSize: 12, color: Color(0XFF0A72D9)))),
                      ),
                    ],
                  )),
              Container(
                  margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  decoration: BoxDecoration(
                      border:
                          Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                  child: TextField(
                    obscureText:true,
                    controller: controller,
                    style: TextStyle(fontSize: 14),
                    decoration: InputDecoration(
                      // suffixIcon: IconButton(
                      //   // 使用IconButton作为后缀图标按钮
                      //   icon: Icon(
                      //     size: 15,
                      //     _obscureText
                      //         ? Icons.visibility_off
                      //         : Icons.visibility, // 根据_obscureText的值切换图标
                      //   ),
                      //   onPressed: () {}, // 点击图标时调用_toggleVisibility方法
                      // ),
                      isCollapsed: true, //设置高度
                      contentPadding: EdgeInsets.all(15),
                      hintStyle: TextStyle(color: Color(0xff999999)),
                      border: InputBorder.none, // 隐藏输入框默认边框
                      hintText: '请输入新密码', // 提示文本
                    ),
                  )),
              Container(
                  margin: EdgeInsets.symmetric(horizontal: 30),
                  decoration: BoxDecoration(
                      border:
                          Border(bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                  child: TextField(
                     obscureText:true,
                    controller: controller1,
                    style: TextStyle(fontSize: 14),
                    decoration: InputDecoration(
                      // suffixIcon: IconButton(
                      //   // 使用IconButton作为后缀图标按钮
                      //   icon: Icon(
                      //     size: 15,
                      //     _obscureText
                      //         ? Icons.visibility_off
                      //         : Icons.visibility, // 根据_obscureText的值切换图标
                      //   ),
                      //   onPressed: () {}, // 点击图标时调用_toggleVisibility方法
                      // ),
                      isCollapsed: true, //设置高度
                      contentPadding: EdgeInsets.all(15),
                      hintStyle: TextStyle(color: Color(0xff999999)),
                      border: InputBorder.none, // 隐藏输入框默认边框
                      hintText: '请输入确认密码', // 提示文本
                    ),
                  )),
              SizedBox(
                height: 20,
              ),
              GestureDetector(
                onTap: () {
                  if (phone.text.isEmpty) {
                    Get.find<HTTPS>().showTost('输入手机号');
                    return;
                  }
                  if (code.text.isEmpty) {
                    Get.find<HTTPS>().showTost('输入验证码');
                    return;
                  }
                  if (controller.text.length < 5) {
                    Get.find<HTTPS>().showTost('密码长度过低');
                    return;
                  }
                  if (controller.text.isEmpty) {
                    Get.find<HTTPS>().showTost('输入密码');
                    return;
                  }
                  if (controller1.text != controller.text) {
                    Get.find<HTTPS>().showTost('密码不一致');
                    return;
                  }
                  Get.find<HTTPS>().postData('/user/passwordByPhone', {
                    "password": controller.text,
                    "password_2": controller1.text,
                    "phone_code": code.text,
                    "phone":phone.text
                  }).then((res) {
                    if (res != null) {
                      Get.find<HTTPS>().showTost('修改成功');

                        Timer(Duration(seconds: 1), () {
                            Get.offNamed('/login/login_type');
                         });
                    }
                  });
                },
                child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 40, vertical: 20),
                    height: 50,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular((50)),
                      gradient: LinearGradient(
                        colors: [
                          Color(0xff2189E7),
                          Color(0xffBF67F8),
                        ], // 定义颜色列表
                        begin: Alignment.topLeft, // 渐变开始的位置
                        end: Alignment.bottomRight, // 渐变结束的位置
                      ),
                    ),
                    child: Text('确认',
                        style:
                            TextStyle(fontSize: 16, color: Color(0XFFFFFFFF)))),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text('已有账户？ 请', style: TextStyle(fontSize: 14)),
                  GestureDetector(
                    onTap: () {
                      Get.toNamed('/login/login_type');
                    },
                    child: Text('登录',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF0A72D9))),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    ));
  }
}
